package com.dayu.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayu.common.utils.PageUtils;
import com.dayu.common.utils.Query;
import com.dayu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.dayu.gulimall.product.dao.AttrDao;
import com.dayu.gulimall.product.dao.AttrGroupDao;
import com.dayu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.dayu.gulimall.product.entity.AttrEntity;
import com.dayu.gulimall.product.entity.AttrGroupEntity;
import com.dayu.gulimall.product.service.AttrGroupService;
import com.dayu.gulimall.product.vo.AttrGroupWithAttrVos;
import com.dayu.gulimall.product.vo.SkuItemVo;
import com.dayu.gulimall.product.vo.SpuItemGroupAttrVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationDao relationDao;

    @Autowired
    private AttrDao attrDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (catelogId != 0) {
            wrapper.eq("catelog_id", catelogId);
        }
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)) {
            //可以接收函数式编程，让sql里面的查询用括号包起来
            // SELECT * FROM pms_attr_group WHERE (catelog_id = ? AND (attr_group_id = ? OR attr_group_name LIKE ?))
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    /**
     * 查询分类下的所有分组以及分组下的所有属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrVos> getAttrGroupWithAttrsByCatelogId(Long catelogId) {

        List<AttrGroupEntity> groupEntityList = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        List<AttrGroupWithAttrVos> collect = groupEntityList.stream().map(item -> {
            AttrGroupWithAttrVos attrGroupWithAttrVos = new AttrGroupWithAttrVos();
            BeanUtils.copyProperties(item,attrGroupWithAttrVos);
            Long attrGroupId = item.getAttrGroupId();
            List<AttrAttrgroupRelationEntity> relationEntities = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", item.getAttrGroupId()));
            if(!CollectionUtils.isEmpty(relationEntities)){
                //获取attrId集合
                List<Long> attrIds = relationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
                List<AttrEntity> attrs = attrDao.selectList(new QueryWrapper<AttrEntity>().in("attr_id", attrIds));
                attrGroupWithAttrVos.setAttrs(attrs);
            }
            return attrGroupWithAttrVos;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SpuItemGroupAttrVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        List<SpuItemGroupAttrVo> attrVos=this.baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
        return attrVos;
    }

}